<?php namespace Phpcmf\Controllers;

class Show extends \Phpcmf\App
{

    public function index() {

        $username = \Phpcmf\Service::L('input')->get('username');
        if ($username) {
            $uid = \Phpcmf\Service::M('member')->uid($username);
            if (!$uid) {
                $this->goto_404_page(dr_lang('用户(#%s)不存在', $username));
            }
        } else {
            $id = (int)\Phpcmf\Service::L('input')->get('id');
            $uid = (int)\Phpcmf\Service::L('input')->get('uid');
            !$uid && $uid = $id;
        }

        if ($uid) {
            $member = dr_member_info($uid);
            if (!$member) {
                $this->goto_404_page(dr_lang('用户(#%s)不存在', $uid));
            }
        } else {
            $this->goto_404_page(dr_lang('uid参数不存在'));
        }

        $seo = [];
        $rep = new \php5replace($member);
        $config = \Phpcmf\Service::M('app')->get_config('huiyuan');
        $seo['meta_title'] = preg_replace_callback('#{([a-z_0-9]+)}#U', array($rep, 'php55_replace_data'), (string)$config['home']['title']);
        $seo['meta_title'] = preg_replace_callback('#{([A-Z_]+)}#U', array($rep, 'php55_replace_var'), $seo['meta_title']);

        $seo['meta_keywords'] = preg_replace_callback('#{([a-z_0-9]+)}#U', array($rep, 'php55_replace_data'), (string)$config['home']['keywords']);
        $seo['meta_keywords'] = preg_replace_callback('#{([A-Z_]+)}#U', array($rep, 'php55_replace_var'), $seo['meta_keywords']);

        $seo['meta_description'] = preg_replace_callback('#{([a-z_0-9]+)}#U', array($rep, 'php55_replace_data'), (string)$config['home']['description']);
        $seo['meta_description'] = preg_replace_callback('#{([A-Z_]+)}#U', array($rep, 'php55_replace_var'), $seo['meta_description']);

        \Phpcmf\Service::V()->assign([
            'uid' => $member['uid'],
            'user' => $member,
            'myuid' => $member['uid'],
        ]);
        \Phpcmf\Service::V()->assign($seo);
        \Phpcmf\Service::V()->module(APP_DIR);
        \Phpcmf\Service::V()->display('show.html');

    }
	public function quan() {
	
	    $username = \Phpcmf\Service::L('input')->get('username');
	    if ($username) {
	        $uid = \Phpcmf\Service::M('member')->uid($username);
	        if (!$uid) {
	            $this->goto_404_page(dr_lang('用户(#%s)不存在', $username));
	        }
	    } else {
	        $id = (int)\Phpcmf\Service::L('input')->get('id');
	        $uid = (int)\Phpcmf\Service::L('input')->get('uid');
	        !$uid && $uid = $id;
	    }
	
	    if ($uid) {
	        $member = dr_member_info($uid);
	        if (!$member) {
	            $this->goto_404_page(dr_lang('用户(#%s)不存在', $uid));
	        }
	    } else {
	        $this->goto_404_page(dr_lang('uid参数不存在'));
	    }
	
	    $seo = [];
	    $rep = new \php5replace($member);
	    $config = \Phpcmf\Service::M('app')->get_config('huiyuan');
	    $seo['meta_title'] = preg_replace_callback('#{([a-z_0-9]+)}#U', array($rep, 'php55_replace_data'), (string)$config['home']['title']);
	    $seo['meta_title'] = preg_replace_callback('#{([A-Z_]+)}#U', array($rep, 'php55_replace_var'), $seo['meta_title']);
	
	    $seo['meta_keywords'] = preg_replace_callback('#{([a-z_0-9]+)}#U', array($rep, 'php55_replace_data'), (string)$config['home']['keywords']);
	    $seo['meta_keywords'] = preg_replace_callback('#{([A-Z_]+)}#U', array($rep, 'php55_replace_var'), $seo['meta_keywords']);
	
	    $seo['meta_description'] = preg_replace_callback('#{([a-z_0-9]+)}#U', array($rep, 'php55_replace_data'), (string)$config['home']['description']);
	    $seo['meta_description'] = preg_replace_callback('#{([A-Z_]+)}#U', array($rep, 'php55_replace_var'), $seo['meta_description']);
	
	    \Phpcmf\Service::V()->assign([
	        'uid' => $member['uid'],
	        'user' => $member,
	        'myuid' => $member['uid'],
	    ]);
	    \Phpcmf\Service::V()->assign($seo);
	    \Phpcmf\Service::V()->module(APP_DIR);
	    \Phpcmf\Service::V()->display('show.html');
	
	}
	
	public function simi() {
		$username = \Phpcmf\Service::L('input')->get('username');
		if ($username) {
		    $uid = \Phpcmf\Service::M('member')->uid($username);
		    if (!$uid) {
		        $this->goto_404_page(dr_lang('用户(#%s)不存在', $username));
		    }
		} else {
		    $id = (int)\Phpcmf\Service::L('input')->get('id');
		    $uid = (int)\Phpcmf\Service::L('input')->get('uid');
		    !$uid && $uid = $id;
		}
			
		if ($uid) {
		    $member = dr_member_info($uid);
		    if (!$member) {
		        $this->goto_404_page(dr_lang('用户(#%s)不存在', $uid));
		    }
		} else {
		    $this->goto_404_page(dr_lang('uid参数不存在'));
		}
		\Phpcmf\Service::V()->assign([
		    'uid' => $member['uid'],
		    'user' => $member,
		    'myuid' => $member['uid'],
		]);
		\Phpcmf\Service::V()->assign($seo);
		\Phpcmf\Service::V()->module(APP_DIR);
		\Phpcmf\Service::V()->display('show.html');
	}

    public function is_friend() {

        if (!$this->uid) {
            $this->_json(0, dr_lang('你没有登录，无法进行关注操作'));
        }

        $uid = (int)\Phpcmf\Service::L('input')->get('uid');
        if (!$uid) {
            $this->_json(0, dr_lang('uid不能为空'));
        } elseif ($uid == $this->uid) {
            $this->_json(0, dr_lang('不能关注自己'));
        }

        $rt = dr_hy_is_friend($uid, $this->uid);
        if ($rt) {
            $this->_json(1, '已关注', [
                'status' => $rt,
                'value' => $rt == 2 ? '相互关注' : '已关注',
            ]);
        } else {
            $this->_json(1, '未关注', [
                'status' => 0,
                'value' => '未关注',
            ]);
        }
    }

    public function friend() {

        if (!$this->uid) {
            $this->_json(0, dr_lang('你没有登录，无法进行关注操作'));
        }

        $uid = (int)\Phpcmf\Service::L('input')->get('uid');
        if (!$uid) {
            $this->_json(0, dr_lang('uid不能为空'));
        } elseif ($uid == $this->uid) {
            $this->_json(0, dr_lang('不能关注自己'));
        }

        $row = \Phpcmf\Service::M()->table('member_friend')->where('uid', $this->uid)->where('touid', $uid)->getRow();
        if (!$row) {
            // 加关注
            $double = \Phpcmf\Service::M()->table('member_friend')->where('uid', $uid)->where('touid', $this->uid)->getRow();
            \Phpcmf\Service::M()->table('member_friend')->insert([
                'uid' => $this->uid,
                'touid' => $uid,
                'isdouble' => $double ? 1 : 0,
                'inputtime' => SYS_TIME,
            ]);
            $double && \Phpcmf\Service::M()->table('member_friend')->update($double['id'], [
                'isdouble' => 1,
            ]);
            $this->_json(1, '关注成功', [
                'status' => $double ? 2 : 1,
                'value' => $double ? '相互关注' : '关注成功',
            ]);
        } else {
            // 取消关注
            \Phpcmf\Service::M()->table('member_friend')->delete($row['id']);
            \Phpcmf\Service::M()->table('member_friend')->where('uid', $uid)->where('touid', $this->uid)->update(0, [
                'isdouble' => 0,
            ]);
            $this->_json(1, '取消关注', [
                'status' => 0,
                'value' => '取消关注',
            ]);
        }

    }
}
